import {getAllGoods,getAllProjects,serachByKeyword} from './function.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    project:false,
    goodList:[],
    projectList:[],
    isDonateGood: false,    // 标志位，判断是否是 donateGood 调用
    isDonateProject: false,    // 标志位，判断是否是 donateProject 调用
    keyword: '',
    sign : false, //标志位，判断onSearch 是否调用
  },

  changeToItem: function(){
    this.setData({project : false})
  },
  
  changeToProject: function(){
    this.setData({project : true})
  },

  async init(event,page=0){
    console.log(page)
    var res=await getAllGoods("ourOx68K-yzP1JGUbwnM9FAEJ8hA",page);
    //console.log(res.data)
    var oldGoodList=this.data.goodList;
    var newGoodList=oldGoodList.concat(res.data);
    //console.log(newGoodList)
    this.setData({
      goodList:newGoodList,
      //projectList:[],
    })
  },

 
  async getProjects(event,page1=0){
    console.log(page1+"--1")
    var res =await getAllProjects("ourOx68K-yzP1JGUbwnM9FAEJ8hA",page1);
    //console.log(res.data);
    var oldProjectList=this.data.projectList;
    var newProjectList=oldProjectList.concat(res.data);
    console.log(newProjectList)
    this.setData({
      projectList:newProjectList,
      //goodList:[],
    })
  },

  donateGood(event) {
    //console.log(event);
    const good = event.currentTarget.dataset.item;
    //console.log(good);
    wx.navigateTo({
      url: '/pages/supplies_contribution/index?goodInfo=' + encodeURIComponent(JSON.stringify(good)),
    });
    this.setData({
      isDonateGood: true,    // 设置标志位为 true，表示是 donateGood 调用
      isDonateProject: false,    // 设置标志位为 false，表示不是 donateProject 调用
    });
    
  },

  donateProject(event) {
    //console.log(event);
    const project = event.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/projects_contribution/index?projectInfo=' + encodeURIComponent(JSON.stringify(project)),
    });
    this.setData({
      isDonateGood: false,    // 设置标志位为 true，表示是 donateGood 调用
      isDonateProject: true,    // 设置标志位为 false，表示不是 donateProject 调用
    });
  },


  onChange(event){
    console.log(event.detail)
    this.setData({
      keyword: event.detail
    });
  },

  async onSearch(){
    // console.log('搜索关键词：', this.data.keyword);
    var res = await serachByKeyword(this.data.keyword);
    console.log(res);
    this.setData({
      goodList:res,
      projectList:[],
      sign :true ,
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init();
    this.getProjects();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  async onReachBottom() {
    if (this.data.goodList.length > 0) {
      var page1 = this.data.goodList.length;
      if(this.data.sign === false ){
        await this.init(page1,page1);
      }
    }
    if (this.data.projectList.length > 0) {
      var page2 = this.data.projectList.length;
      console.log(page2+"--2");
      await this.getProjects(page2,page2);
    }
    if (this.data.goodList.length == 0 && this.data.projectList.length == 0) {
      await this.init(0); // 如果两个数组都为空，则重新初始化第一页数据
    }
    

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
    if (this.data.isDonateGood) {
      this.data.goodList=[];
      console.log(this.data.isDonateGood)
      this.init();   
    } else if (this.data.isDonateProject) {
      this.data.projectList=[];
      this.getProjects();   
    }
    // 重置标志位为初始值
    this.setData({
      isDonateGood: false,
      isDonateProject: false,
      sign :false,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },


})
